/*
 * @Description:
 * @Author: 月间
 * @Date: 2024-10-14 21:07:53
 * @LastEditTime: 2024-10-15 23:23:33
 * @LastEditors:
 */
import { request } from '../request';

export interface LoginParams {
  username: string;
  password: string;
  code?: string;
  uuid?: string;
  method: string;
}

/**
 * Login
 *
 * @param params Login parameters
 */
export function fetchLogin(params: LoginParams) {
  return request<Api.Auth.LoginToken>({
    url: '/auth/login',
    method: 'post',
    data: params
  });
}

/** Get user info */
export function fetchGetUserInfo() {
  return request<Api.Auth.UserInfo>({ url: '/auth/getUserInfo' });
}

/**
 * Refresh token
 *
 * @param refreshToken Refresh token
 */
export function fetchRefreshToken(refreshToken: string) {
  return request<Api.Auth.LoginToken>({
    url: '/auth/refreshToken',
    method: 'post',
    data: {
      refreshToken
    }
  });
}

/**
 * return custom backend error
 *
 * @param code error code
 * @param msg error message
 */
export function fetchCustomBackendError(code: string, msg: string) {
  return request({ url: '/auth/error', params: { code, msg } });
}

/**
 * Change password
 * 
 * @param oldPassword Old password
 * @param newPassword New password
 */
export function fetchChangePassword(oldPassword: string, newPassword: string) {
  return request({
    url: '/auth/updatePassword',
    method: 'post',
    data: {
      oldPassword,
      newPassword
    }
  });
}

/**
 * Update user info
 * 
 * @param userInfo User info to update
 */
export function fetchUpdateUserInfo(userInfo: {
  nickname: string;
  phone: string;
  email: string;
  sex: string;
  remark: string;
}) {
  return request({
    url: '/auth/updateUserInfo',
    method: 'post',
    data: userInfo
  });
}

/**
 * Get captcha image
 * 
 * @returns Captcha image and uuid
 */
export function getCaptchaImage() {
  return request<Api.Auth.Captcha>({
    url: '/auth/captchaImage',
    method: 'get'
  });
}
